package con.shake;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class MyService extends Service {

	private static final String ACTION_WIFI = "wifi";
	private static final String ACTION_3G = "3g";
	private static final String ACTION_CALL = "call";

	private ShakeListener mShaker;
	private SharedPreferences sharedPreference;
	private OnSharedPreferenceChangeListener ospcl;

//	private String action;
	private String phoneNo;
	
	private boolean is3GEnabled;
	private boolean isWiFiEnabled;
	private boolean isCallingEnabled;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		// final Vibrator vibe = (Vibrator)
		// getSystemService(Context.VIBRATOR_SERVICE);
		try {
			mShaker = new ShakeListener(this);
			mShaker.setOnShakeListener(new ShakeListener.OnShakeListener() {
				public void onShake(int direction) {
					try {

						Log.i("DREG", "DIRECTION="+direction);
						
						if(direction == ShakeListener.SHAKE_DIRECTION_X){
							if(is3GEnabled){
								if (Utils.getMobleDataEnabled(MyService.this)) {
									Utils.setMobleDataEnabled(MyService.this, false);
									Utils.startSound(
											MyService.this,
											Uri.parse("android.resource://"
													+ getPackageName()
													+ "/raw/success"));
								} else {
									Utils.setMobleDataEnabled(MyService.this, true);
									Utils.startSound(
											MyService.this,
											Uri.parse("android.resource://"
													+ getPackageName()
													+ "/raw/failure"));
								}
							}
						}else if(direction == ShakeListener.SHAKE_DIRECTION_Y){
							if(isWiFiEnabled){
								if (Utils.getWifiEnabled(MyService.this)) {
									Utils.setWifiEnabled(MyService.this, false);
									Utils.startSound(
											MyService.this,
											Uri.parse("android.resource://"
													+ getPackageName()
													+ "/raw/success"));
								} else {
									Utils.setWifiEnabled(MyService.this, true);
									Utils.startSound(
											MyService.this,
											Uri.parse("android.resource://"
													+ getPackageName()
													+ "/raw/failure"));
								}
							}
						}else if(direction == ShakeListener.SHAKE_DIRECTION_Z){
							if(isCallingEnabled){
								if (phoneNo != null && phoneNo.length() > 0) {
									try {
										Intent intent = new Intent(
												Intent.ACTION_CALL, Uri
														.parse("tel:" + phoneNo));
										intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
										startActivity(intent);
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							}
						}
						
//						if (ACTION_WIFI.equals(action)) {
//							if (Utils.getWifiEnabled(MyService.this)) {
//								Utils.setWifiEnabled(MyService.this, false);
//								Utils.startSound(
//										MyService.this,
//										Uri.parse("android.resource://"
//												+ getPackageName()
//												+ "/raw/success"));
//							} else {
//								Utils.setWifiEnabled(MyService.this, true);
//								Utils.startSound(
//										MyService.this,
//										Uri.parse("android.resource://"
//												+ getPackageName()
//												+ "/raw/failure"));
//							}
//						} else if (ACTION_3G.equals(action)) {
//							if (Utils.getMobleDataEnabled(MyService.this)) {
//								Utils.setMobleDataEnabled(MyService.this, false);
//								Utils.startSound(
//										MyService.this,
//										Uri.parse("android.resource://"
//												+ getPackageName()
//												+ "/raw/success"));
//							} else {
//								Utils.setMobleDataEnabled(MyService.this, true);
//								Utils.startSound(
//										MyService.this,
//										Uri.parse("android.resource://"
//												+ getPackageName()
//												+ "/raw/failure"));
//							}
//						} else if (ACTION_CALL.equals(action)) {
//							if (phoneNo != null && phoneNo.length() > 0) {
//								try {
//									Intent intent = new Intent(
//											Intent.ACTION_CALL, Uri
//													.parse("tel:" + phoneNo));
//									intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//									startActivity(intent);
//								} catch (Exception e) {
//									e.printStackTrace();
//								}
//							}
//						}

						// vibe.vibrate(100);
						// boolean wifiEnabled = wifiManager.isWifiEnabled();
						// if (wifiEnabled) {
						// wifiManager.setWifiEnabled(false);
						// } else {
						// wifiManager.setWifiEnabled(true);
						// }

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});

			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(this);

			ospcl = new OnSharedPreferenceChangeListener() {
				public void onSharedPreferenceChanged(
						SharedPreferences sharedPreferences, String key) {
					if (ShakeApp.KEY_IS_WIFI_ENABLED.equals(key)) {
						isWiFiEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_WIFI_ENABLED, false);
					} else if (ShakeApp.KEY_IS_3G_ENABLED.equals(key)) {
						is3GEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_3G_ENABLED, false);
					} else if (ShakeApp.KEY_IS_CALL_ENABLED.equals(key)) {
						isCallingEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_CALL_ENABLED, false);
					} else if (ShakeApp.KEY_CONTACT_NUMBER.equals(key)) {
						phoneNo = sharedPreferences.getString(
								getString(R.string.phone_no_key), null);
					}
				}
			};
			sharedPreference.registerOnSharedPreferenceChangeListener(ospcl);
			isWiFiEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_WIFI_ENABLED, false);
			is3GEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_3G_ENABLED, false);
			isCallingEnabled = sharedPreference.getBoolean(ShakeApp.KEY_IS_CALL_ENABLED, false);
			
			phoneNo = sharedPreference.getString(ShakeApp.KEY_CONTACT_NUMBER, null);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDestroy() {
		try {
			mShaker.destroy();
			if ((sharedPreference != null) && (ospcl != null)) {
				sharedPreference
						.unregisterOnSharedPreferenceChangeListener(ospcl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startid) {
	}

}
